<template>
  <el-row type="flex" justify="space-between" align="middle" style="height:40px;width:100%">
    <el-col>{{ nodeTree.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end" align="middle">
        <el-col>
          {{ nodeTree.manager }}
        </el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown @command="handleCommand">
            <span>操作
              <i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu>
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 导入删除子部门方法
import { DelDepartmentsAPI } from '@/api'
export default {
  name: 'TreeTool',
  props: {
    // 节点数据
    nodeTree: {
      type: Object,
      required: true
    },
    // 是否是根节点
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'add') {
        // 添加子部门
        this.$emit('addDepts', this.nodeTree)
      } else if (command === 'edit') {
        // 编辑子部门
        this.$emit('editDepts', this.nodeTree)
      } else if (command === 'del') {
        // 删除子部门
        this.$confirm('确认要删除该部门吗？').then(() => {
          return DelDepartmentsAPI(this.nodeTree.id)
        }).then(() => {
          // 后台数据删了 前台数据怎么发生改变
          this.$emit('DelDepts') // 通知父组件
          this.$message('数据删除成功') // 消息提示
        })
      }
    }
  }

}
</script>

<style>

</style>
